//
//  FJLottieAnimationProtocol.swift
//  FruitJuice
//
//  Created by Rae on 2023/12/20.
//

import UIKit
import Lottie

struct FJLottieAnimationWrapper<Base> {
    let base: Base
    init(_ base: Base) {
        self.base = base
    }
}

protocol FJLottieAnimationProtocol { }
extension FJLottieAnimationProtocol {
    ///
    public var animation: FJLottieAnimationWrapper<Self> {
        get { return FJLottieAnimationWrapper(self) }
        set { }
    }
    
}

typealias FJLottieAnimationView = LottieAnimationView

extension FJLottieAnimationWrapper {
    
    func make(_ json: FJJsonType, loopMode: LottieLoopMode = .loop, speed: CGFloat = 1.0) -> FJLottieAnimationView {
        make(json, loopMode: loopMode, speed: speed, playFinishBlock: nil)
    }
    
    func make(_ name: FJJsonType, loopMode: LottieLoopMode = .loop, speed: CGFloat = 1.0, contentMode: UIView.ContentMode = .scaleAspectFill, playFinishBlock: ((Bool) -> Void)? = nil) -> FJLottieAnimationView {
        let view = FJLottieAnimationView(name: name.rawValue, bundle: .main)
        view.contentMode = contentMode
        view.loopMode = loopMode
        view.shouldRasterizeWhenIdle = true
        view.backgroundBehavior = .pauseAndRestore
        view.currentProgress = 0
        view.animationSpeed = speed
        view.play(completion: playFinishBlock)
        return view
    }
    
}

extension FJLottieAnimationWrapper {
    
    enum FJJsonType: String {
        
        case loading = "FJLoading"
    }
    
}
